<template>
    <div>
        <zh-form ref="fileCollectDataRef" :span="8" :model="pageData.formData" :rules="pageData.rules"  :disabled="props.showFlag" label-width="200px">

           <zh-form-input prop="collectTitle" label="归集标题" type="text" :span="8" dataType="common"
                    v-model="pageData.formData.collectTitle"></zh-form-input>
           <zh-form-input prop="parentCode" label="父类编号" type="text" :span="8" dataType="common" :disabled="true" v-if="false"
                    v-model="pageData.formData.parentCode"></zh-form-input> 
           <zh-form-input prop="parentcollectTitle" label="父类归集标题" type="text" :span="8" dataType="common" :disabled="true"
                    v-model="pageData.formData.parentcollectTitle"></zh-form-input>                   
            <zh-form-select prop="status" label="有效状态" allowClear :span="8" :fieldNames="{label: 'dictValue', value: 'dictKey'}" :options="pageData.dictData.STATUS"
                    v-model="pageData.formData.status"></zh-form-select>  

            <zh-form-input prop="archivesCollectCode" label="对应档案分类编号" type="text" :span="8" dataType="common"
                    v-model="pageData.formData.archivesCollectCode"></zh-form-input> 

            <zh-form-date
				prop="expireTime"
				label="过期时间"
				placeholder="请输入默认访问过期时间"
				type="datetime"
				v-model="pageData.formData.expireTime"
			></zh-form-date>

            <zh-form-select
                  prop="secrecyLevel"
                  label="保密级别"
                  allowClear :span="8"
                  :options="pageData.dictData.SECRECYLEVEL"
                  v-model="pageData.formData.secrecyLevel"
              ></zh-form-select>

              <zh-form-input type="textarea" prop="remarks" label="备注"  v-model="pageData.formData.remarks" :span="16"
                     ></zh-form-input>
        
        </zh-form>

    </div>
</template>
<script lang="ts" setup name="FileCollectDataForm">
import { ElMessage } from 'element-plus';
//------------------接收传入参数(组件)-------------------
const props = defineProps({
    FileCollectData: {
        type: Object,
        default: () => { },
        required: false,
    },
    showFlag: {
        type: Boolean,
        default: false
    },
    modelValue: Boolean,
});
//------------------定义子组件---------------------------
const fileCollectDataRef = ref()
const emit = defineEmits(['update:modelValue']);


//------------------内部变量定义-------------------------
const pageData = reactive({
    formData: {
        fileCollectCode:'',
        collectTitle:'',
        parentCode:'',
        status:'',
        archivesCollectCode:'',
        expireTime:'',
        secrecyLevel:'',
        remarks:'',
        parentcollectTitle:''
    },
    dictFields: 'STATUS,SECRECYLEVEL',
    dictData: {} as any,
    rules: reactive({

        fieldType:[
            {
                required:true,
                message:'请输入字段类型',
                trugger:'blur'
            }
        ],
        

    }),
    editFlag: false,
    codeDisabled: false,
})
//------------------定义调用方法-------------------------
function getDict() {
    setTimeout(() => {
        let params = {
            dictFields: pageData.dictFields,
        };
        $api.common.getBatchDict.post(params).then((res: any) => {
            pageData.dictData = res.result;
        });

    }, 20);
}

function getDetail() {  
    //1查询,4删除，5修改
    if(props.FileCollectData.funFlag==='1'||props.FileCollectData.funFlag==='4'||props.FileCollectData.funFlag==='5'){
        if (!props.FileCollectData || !props.FileCollectData.fileCollectCode) {        
            return
        } else {
            let params = { fileCollectCode: props.FileCollectData.fileCollectCode };
            $api.common.fileSend.post('fileCollect0002', params).then((res) => {
                pageData.codeDisabled = true
                pageData.formData = Object.assign({}, res.result)
                // console.log('父级编号：'+pageData.formData.parentCode)
                if(pageData.formData.parentCode!==''&&pageData.formData.parentCode!==null&&pageData.formData.parentCode!==undefined){
                    let param_parent = { fileCollectCode: pageData.formData.parentCode };
                    $api.common.fileSend.post('fileCollect0002', param_parent).then((res) => {
                        pageData.formData.parentcollectTitle=res.result.collectTitle;
                    })
                }
            })
            
            
        }
    }
    //子级新增
    if(props.FileCollectData.funFlag==='2'){
        pageData.formData.parentCode = props.FileCollectData.fileCollectCode;
        pageData.formData.status='1';
        let param_parent = { fileCollectCode: pageData.formData.parentCode };
        $api.common.fileSend.post('fileCollect0002', param_parent).then((res) => {
            pageData.formData.parentcollectTitle=res.result.collectTitle;
        })
    }
    //根目录新增
    if(props.FileCollectData.funFlag==='3'){
        pageData.formData.status='1';
    }     
    
}

async function save() {
    //校验表单
    let checkFlag = await fileCollectDataRef.value.checkForm(); 
    if (checkFlag) {
        // if(pageData.formData.expireTime!==''&&pageData.formData.expireTime!==null&&pageData.formData.expireTime!==undefined){
        //     pageData.formData.expireTime=pageData.formData.expireTime+' 23:59:59';
        // }
        
        const info = pageData.formData;
        let ret = { saveType: '', code: 0 };
        //删除
        if(props.FileCollectData.funFlag==='4'){
            ret = await $api.common.fileSend.post('fileCollect0006', info);
            ret.saveType = 'add';
            if (ret.code === 0) {
                ElMessage.success('删除成功！');
                emit('update:modelValue', false);
            }
        }
        //2：子级新增，3：//根目录新增
        if(props.FileCollectData.funFlag==='2'||props.FileCollectData.funFlag==='3'){
            ret = await $api.common.fileSend.post('fileCollect0004', info);
            ret.saveType = 'add';
            if (ret.code === 0) {
                ElMessage.success('新增成功！');
                emit('update:modelValue', false);
            }
        }  
        //5修改  
        if(props.FileCollectData.funFlag==='5'){
            ret = await $api.common.fileSend.post('fileCollect0005', info);
            ret.saveType = 'add';
            if (ret.code === 0) {
                ElMessage.success('修改成功！');
                emit('update:modelValue', false);
            }
        }    
        
        return ret;
    }
    return null;

}

//------------------初始化------------------------------
onMounted(() => {
    getDict();
    getDetail();
})
if (props.FileCollectData) {
    if (props.FileCollectData.fileCollectCode) {
        pageData.codeDisabled = true
    }
}
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: pageData.formData, save });
</script>
<style lang="scss" scoped>

</style>

